<template>
    <view>
        <u-toast ref="uToast" /><u-no-network></u-no-network>
        <u-navbar title="设置密码" safeAreaInsetTop fixed placeholder>
            <view class="coreshop-navbar-left-slot" slot="left">
                <u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
                <u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
                <u-icon name="home" size="22" @click="goHome"></u-icon>
            </view>
            <view slot="right">
            </view>
        </u-navbar>
        <view class="coreshop-padding-15 coreshop-bg-white">
            <u--form :model="model" :rules="rules" ref="uForm" errorType="message" labelPosition="left" labelWidth="80">
                <u-form-item label="旧密码" prop="pwd" v-if="oldpassWord" borderBottom>
                    <u--input border="none" :password-icon="true" type="password" v-model="model.pwd" placeholder="请输入密码"></u--input>
                </u-form-item>
                <u-form-item label="新密码" prop="newPwd" borderBottom>
                    <u--input border="none" :password-icon="true" type="password" v-model="model.newPwd" placeholder="请输入密码"></u--input>
                </u-form-item>
                <u-form-item label="确认密码" prop="rePwd" borderBottom>
                    <u--input border="none" :password-icon="true" type="password" v-model="model.rePwd" placeholder="请确认密码"></u--input>
                </u-form-item>
            </u--form>
        </view>

        <!--按钮-->
        <view class="coreshop-bg-white coreshop-footer-fixed coreshop-foot-padding-bottom">
            <u-button type="error" size="normal" @click="submitHandler" :disabled='submitStatus' :loading='submitStatus'>保存</u-button>
        </view>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                model: {
                    pwd: '',
                    newPwd: '',
                    rePwd: '',
                },
                submitStatus: false,
                oldpassWord: true,
                rules: {
                    pwd: [
                        {
                            type: 'string',
                            required: true,
                            message: '请输入密码',
                            trigger: ['change', 'blur'],
                        }
                    ],
                    newPwd: [
                        {
                            type: 'string',
                            required: true,
                            message: '请输入密码',
                            trigger: ['change', 'blur'],
                        }
                    ],
                    rePwd: [
                        {
                            type: 'string',
                            required: true,
                            message: '请重新输入密码',
                            trigger: ['change', 'blur'],
                        },
                        {
                            validator: (rule, value, callback) => {
                                return value === this.model.newPwd;
                            },
                            message: '两次输入的密码不相等',
                            trigger: ['change', 'blur'],
                        }
                    ],
                },
            }
        },
        computed: {},
        onReady() {
            this.$refs.uForm.setRules(this.rules);
        },
        methods: {
            // 保存资料
            submitHandler() {
                this.submitStatus = true;

                this.$refs.uForm.validate().then(res => {

                    this.$u.api.editPwd({
                        pwd: this.model.pwd,
                        newpwd: this.model.newPwd,
                        repwd: this.model.rePwd
                    }).then(res => {
                        this.submitStatus = false;
                        if (res.status) {
                            this.$refs.uToast.show({message: res.msg, type: 'success', complete: function () {
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }
                            })

                        } else {
                            this.$u.toast(res.msg)
                        }
                    });
                }).catch(errors => {
                    uni.$u.toast('您输入的内容校验失败。')
                })
            }
        },
        onLoad: function () {
            var _this = this;
            _this.$u.api.userInfo().then(res => {
                if (res.status) {
                    _this.oldpassWord = res.data.passWord ? true : false;;
                } else {
                    //报错了
                    _this.$u.toast(res.msg);
                }
            });
        }
    }
</script>

<style lang="scss">
</style>
